package top.wardyan.test.ctrl;

import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;

import com.google.gson.JsonParser;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.Prop;
import com.lark.oapi.Client;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.im.v1.model.ListMessageReq;
import com.lark.oapi.service.im.v1.model.ListMessageResp;

import cn.hutool.core.lang.Console;
import top.wardyan.base.common.config.ConfigProp;
import top.wardyan.base.common.util.DateUtil;
import top.wardyan.common.ctrl.BaseController;
import top.wardyan.wtask.feishu.api.FeishuMsgBusiness;
import top.wardyan.wtask.tapd.api.TapdBusiness;


@Path("/test")
public class TTestController extends BaseController{

	private static final cn.hutool.log.Log log = cn.hutool.log.Log.get();
	
	private static final Prop PROP = ConfigProp.load();
	
	
	@Inject FeishuMsgBusiness feishuMsgBusiness;
	@Inject TapdBusiness tapdBusiness;
	
	
	public void testsubstring() {
		String s = "这是一个注释--234";
		Console.log(s.substring(0, s.indexOf("--")));
		this.renderNull();
	}
	
	public void tapdtask() {
		Timestamp begin = DateUtil.parse("2024-12-01 00:00:00");
		Timestamp end = DateUtil.parse("2024-12-10 23:59:59");
		tapdBusiness.fetchTask(begin, end);
		this.renderNull();
	}
	
	
	public void tapd() {
		tapdBusiness.fetchStory(null, null);
		this.renderNull();
	}
	

	public void spy() {
		
		//String name = feishuMsgBusiness.getManNameFromFeishu("ou_646ca7deaf9c830cc51e7191401ff305");
		// String token = feishuMsgBusiness.getToken();
		Timestamp begin = DateUtil.parse("2024-11-01 00:00:00");
		feishuMsgBusiness.spy(begin, true, "");
		this.renderText("");
	}
	
	
	public void feishu() {
		
		String name = feishuMsgBusiness.getManNameFromFeishu("ou_646ca7deaf9c830cc51e7191401ff305");
		// String token = feishuMsgBusiness.getToken();
		this.renderText(name);
	}
	
	public void index() {
		Console.log("test.index");
		
		String datas = this.getRawData();
		
		this.getJson(null);
		
		Console.log(datas);
		this.renderNull();
		// this.renderJson(datas);
		// this.renderText(datas);
		
		
	}
	
	
	public void testtapd() throws Exception {
		String APPID = PROP.get("feishu.app_id");
		String SECRET = PROP.get("feishu.app_secret");
		String CONTAINERID = PROP.get("feishu.container_id");

		// SDK 使用文档：https://github.com/larksuite/oapi-sdk-java/tree/v2_main
		// 复制该 Demo 后, 需要将 "YOUR_APP_ID", "YOUR_APP_SECRET" 替换为自己应用的 APP_ID, APP_SECRET.
		// 以下示例代码是根据 API 调试台参数自动生成，如果存在代码问题，请在 API 调试台填上相关必要参数后再使用
				// 构建client
				Client client = Client.newBuilder(APPID, SECRET).build();

				// 创建请求对象
				ListMessageReq req = ListMessageReq.newBuilder()
					.containerIdType("chat")
					.containerId(CONTAINERID)
					.startTime(String.valueOf(Float.valueOf(DateUtil.range(1).getTime() / 1000).intValue()))
					.build();

				// 发起请求
				ListMessageResp resp = client.im().message().list(req);

				// 处理服务端错误
				if(!resp.success()) {
					System.out.println(String.format("code:%s,msg:%s,reqId:%s, resp:%s",
						resp.getCode(), resp.getMsg(), resp.getRequestId(), Jsons.createGSON(true, false).toJson(JsonParser.parseString(new String(resp.getRawResponse().getBody(), StandardCharsets.UTF_8)))));
					return;
				}

				// 业务数据处理
				System.out.println(Jsons.DEFAULT.toJson(resp.getData()));

		this.renderNull();
	}
}
